﻿using Com.Mc.Common.FaceEnginePoll;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;

namespace One2One
{
    public static class Util
    {
        public static byte[] GetImageByUrl(string url)
        {
            try
            {

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse response;
                try
                {
                    response = (HttpWebResponse)request.GetResponse();
                }
                catch (Exception)
                {
                    return null;
                }

                if ((response.StatusCode == HttpStatusCode.OK ||
        response.StatusCode == HttpStatusCode.Moved ||
        response.StatusCode == HttpStatusCode.Redirect) &&
        response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))
                {

                    // if the remote file was found, download it
                    using (Stream stream = response.GetResponseStream())
                    {
                        List<byte> bytes = new List<byte>();
                        int temp = stream.ReadByte();
                        while (temp != -1)
                        {
                            bytes.Add((byte)temp);
                            temp = stream.ReadByte();
                        }

                        return bytes.ToArray();
                    }
                }
                else
                    return null;

            }
            catch (Exception ex)
            {
                return null;
            }
            
   
        }

        public static float GetSimilar(byte[] image1, byte[] image2) 
        {
            if (image1 != null && image2 != null)
            {
                var enginge = LinqFaceEnginePollManager<FaceEnginePollItem>.Instance.GetFaceEngineItem(true);
                if (enginge != null && enginge.OneToOneAlgorithm != null)
                {
                    try
                    {
                        return enginge.OneToOneAlgorithm.CompareFacesWithImageData(image1, image2);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        LinqFaceEnginePollManager<FaceEnginePollItem>.Instance.Release(enginge);
                    }
                }
                else
                {
                    throw new ArgumentException("未获取到引擎");
                }
                
            }
            return -100;
        }

        public static string GetErrorInfor(float errCode)
        {
            string result="未知错误";
            switch ((int)errCode)
            {
                case -1:
                    result = "Model不存在";
                    break;
                case -2:
                    result = "不支持此图片格式";
                    break;
                case -3:
                    result = "图片解析错误";
                    break;
                case -4:
                    result = "读取图片错误";
                    break;
                case -5:
                    result = "识别引擎创建失败";
                    break;
                case -6:
                    result = "句柄Handle错误";
                    break;
                case -7:
                    result = "人脸检测错误";
                    break;
                case -8:
                    result = "特征点获取失败";
                    break;


            }
            return result;
        }
    }
}